//
//  ViewController.m
//  日历
//
//  Created by apple on 15/8/27.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

{

    NSMutableArray *array;
    CGFloat _width;
    CGFloat _height;
    
}

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSDate *today = [NSDate date];
//    显示label今天是哪一年几月几号星期几；
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"今天yyyy年MM月dd日cccc";//星期几用“cccc”；
    _dateLabel.text = [dateFormatter stringFromDate:today];
    
    //    获取屏幕大小；
    CGRect rect = [UIScreen mainScreen].bounds;
    NSLog(@"%@",NSStringFromCGRect(rect));
    
//   算出矩形的大小；
    CGFloat width = (rect.size.width - 4*8)/7;
    CGFloat height = (rect.size.height - 60 - 10 - 4*4)/5;
    
    for (int i = 0; i < 31; i++)
    {
        int line = i/7;    //行
        int column = i%7;  //列
        
        CGFloat x = 4 + (width + 4)* column;
        CGFloat y = 60 +(height + 4)*line;
        
//        构造label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        
//        设置label的颜色；
        label.backgroundColor = [UIColor brownColor];
        
//        label中文字对齐的方式；
        label.textAlignment = NSTextAlignmentCenter;
//        不限制行数，只要label的高度足够；
        label.numberOfLines = 0;
//        label输出
        label.text = [NSString stringWithFormat:@"%d\n%@",i + 1,@""];
        
//        将label显示在屏幕上；
        [self.view addSubview:label];
    }
    
    

    // Do any additional setup after loading the view, typically from a nib.
}
//获取date所在的月份有多少天；
- (NSInteger)dayOfMonth:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];

    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    return range.length;
}
//获取date是星期几
- (NSString *)weekdayOfDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"cccc";
    
    return [formatter stringFromDate:date];
}

//获取date所在月份的第一天的时间；

- (NSDate *)startDateOfMonth:(NSDate *)date
{
    NSDate *startDay;
    NSTimeInterval ti;
    
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&startDay interval:&ti forDate:date];
    NSLog(@"%@",startDay);
    
    return startDay;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

@end
